package com.security.hikapp.network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

import androidx.lifecycle.LifecycleOwner;


import com.security.hikapp.MyApplication;
import com.security.hikapp.network.response.ICallback;
import com.security.hikapp.network.response.IResponse;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;

/*public class RequestModel {
     public static <T> void request(Observable<? extends IResponse<T>> o,
                                   LifecycleOwner lifecycleOwner,
                                   ICallback<T> callback){
       if (!isNetWorkConnected(BaseActivity.getContext())){
            ToastUtil.toast(BaseActivity.getContext(),"请检查网络连接！");
        }else {
            o.compose(ResponseTransformer.obtain(lifecycleOwner))
            .subscribe(new Consumer<IResponse<T>>() {
                @Override
                public void accept(IResponse<T> tiResponse) throws Throwable {
                    if (tiResponse.isSuccess()) {
                        callback.onSuccess(tiResponse.getData());
                    }else {
                        callback.onFailure(new ApiException(tiResponse.getStatus(), tiResponse.getMessage()), tiResponse.getData());
                    }

                 }
            }, new ErrorConsumer<T>() {
                @Override
                protected void error(ApiException e) {
                    Log.d("RequestModel---", "error: "+e);
                    callback.onFailure(new ApiException(e.getCode(), e.getErrorMsg()), null);
                }
            });
        }
    }

    public static boolean isNetWorkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable() && mNetworkInfo.isConnected();
            }
        }
        return false;
    }


}*/
